package top.lishuoboy.springmvc.a10;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 十、拦截器
 * <p>
 * 2、拦截器的三个抽象方法
 * SpringMVC中的拦截器有三个抽象方法：
 * preHandle：控制器方法执行之前执行preHandle()，其boolean类型的返回值表示是否拦截或放行，返回true为放行，即调用控制器方法；返回false表示拦截，即不调用控制器方法
 * postHandle：控制器方法执行之后执行postHandle()
 * afterComplation：处理完视图和模型数据，渲染视图完毕之后执行afterComplation()
 * http://localhost:8080/lishuoboy_springmvc_war_exploded/a10
 *
 * @author lishuoboy
 */
@Slf4j
@Component
public class FirstInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.error("1拦截器 FirstInterceptor.preHandle()");
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.error("1拦截器 FirstInterceptor.postHandle()");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.error("1拦截器 FirstInterceptor.afterCompletion()");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

}